home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / Examples / DroneZone / DZDisplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-16  |  20.6 KB  |  765 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        DZDisplay.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Contents:    Handles the window and its contents.
  6.  *
  7.  *    Copyright © 1996 Apple Computer, Inc.
  8.  */
  9.  
  10. #ifndef USE_DRAW_SPROCKET
  11.     #define USE_DRAW_SPROCKET        1
  12. #endif
  13.  
  14. #include <assert.h>
  15.  
  16. #include <Fonts.h>
  17. #include <Quickdraw.h>
  18. #include <Windows.h>
  19.  
  20. #include <QD3D.h>
  21. #include <QD3DCamera.h>
  22. #include <QD3DDrawContext.h>
  23. #include <QD3DGroup.h>
  24. #include <QD3DLight.h>
  25. #include <QD3DMath.h>
  26. #include <QD3DRenderer.h>
  27. #include <QD3DShader.h>
  28. #include <QD3DView.h>
  29.  
  30. #include "DZDisplay.h"
  31. #include "DZGame.h"
  32. #include "DZInput.h"
  33. #include "DZResource.h"
  34.  
  35. #if USE_DRAW_SPROCKET
  36.     #include "DrawSprocket.h"
  37. #endif
  38.  
  39.  
  40. #if USE_DRAW_SPROCKET
  41.     static DisplayObject    gDisplayObject                = NULL;
  42. #else
  43.     static WindowPtr        gDisplayWindow                = NULL;
  44. #endif
  45.  
  46. static Boolean                gDisplayActive                = false;
  47.  
  48. static PicHandle            gDisplayStartScreen            = NULL;
  49.  
  50. static TQ3ViewObject        gDisplayView                = NULL;
  51. static TQ3DrawContextObject    gDisplayDrawContext            = NULL;
  52. static TQ3RendererObject    gDisplayRenderer            = NULL;
  53. static TQ3CameraObject        gDisplayCamera                = NULL;
  54. static TQ3GroupObject        gDisplayLightGroup            = NULL;
  55. static TQ3LightObject        gDisplayAmbientLight        = NULL;
  56. static TQ3LightObject        gDisplayDirectionalLight    = NULL;
  57. static TQ3LightObject        gDisplayPointLight            = NULL;
  58. static TQ3ShaderObject        gDisplayIllumination        = NULL;
  59.  
  60. //• To get around an Interactive Renderer bug wherein Q3Renderer_Sync
  61. //•    crashes when called on a renderer that has not rendered.
  62. static Boolean                gDisplayHasRendered            = false;
  63.  
  64.  
  65. #if USE_DRAW_SPROCKET
  66.     static Boolean Display_IsBusy(
  67.         DisplayObject        inDisplayObject,
  68.         UInt32                inContext);
  69. #endif
  70.  
  71. static void Display_RenderImage(
  72.     void);
  73.     
  74. static void Display_BeginQD(
  75.     void);
  76.     
  77. static void Display_EndQD(
  78.     void);
  79.     
  80. static void Display_GetImageArea(
  81.     TQ3Area*                outArea);
  82.  
  83. static float Display_GetImageAspectRatio(
  84.     void);
  85.  
  86.  
  87. /* =============================================================================
  88.  *        Display_Init (external)
  89.  *
  90.  *    Creates the display window, initializes QD3D view etc. to draw in it.
  91.  * ========================================================================== */
  92. void Display_Init(
  93.     void)
  94. {
  95.     TQ3ViewAngleAspectCameraData    viewAngleCameraData;
  96.     TQ3PointLightData                pointLightData;
  97.     TQ3ColorRGB                        white = {1.0, 1.0, 1.0};
  98.     
  99.     // Grab the start screen
  100.     gDisplayStartScreen = GetPicture(kPictID_StartScreen);
  101.     
  102.     // Create the view
  103.     gDisplayView = Q3View_New();
  104.     
  105.     // Set up where to draw
  106.     #if USE_DRAW_SPROCKET
  107.     {
  108.         DisplayConfig                    theConfig;
  109.         DisplayConfig                    theActualConfig;
  110.         
  111.         // Create the display
  112.         theConfig.device                    = NULL;
  113.         theConfig.mode                        = 0;
  114.         theConfig.frequency                    = 0;
  115.         theConfig.width                        = 512;
  116.         theConfig.height                    = 384;
  117.         theConfig.colorNeeds                = kDisplayColorRequest;
  118.         theConfig.colorTable                = NULL;
  119.         theConfig.specialFlags                 = kDisplaySpecialBuffered | kDisplaySpecialQD3DAccel;
  120.         theConfig.specialFlagsInHW             = 0;
  121.         theConfig.frontBufferDepthMask        = kDisplayDepthMaskAll;
  122.         theConfig.backBufferDepthMask        = kDisplayDepthMask32 | kDisplayDepthMask16;
  123.         theConfig.frontBufferBestDepth        = 32;
  124.         theConfig.backBufferBestDepth        = 32;
  125.         theConfig.pageCount                    = 2;
  126.         theConfig.reserved1                    = 0;    
  127.         theConfig.reserved2                    = 0;    
  128.         FindGDeviceFromConfig(&theConfig);
  129.         NewDisplay(&theConfig, &gDisplayObject);
  130.         
  131.         GetDisplayActualConfig(gDisplayObject, &theActualConfig);
  132.         if (theActualConfig.frontBufferBestDepth < 16)
  133.         {
  134.             //• SHOULD PUT UP AN ALERT HERE SAYING SOMETHING LIKE
  135.             //•    "The best monitor setting that DroneZone could find was XXX."
  136.             //• "DroneZone runs much faster in Thousands or Millions of Colors."
  137.             //• "You can run in Thousands or Millions of Colors if you expand"
  138.             //• "the Video RAM on your display card, or if you switch to a smaller"
  139.             //• "video monitor."  (OK) (Quit)
  140.         }
  141.         
  142.         // DSp_SetDebugMode(true);
  143.         
  144.         FadeDisplayGamma(NULL, kSmoothFadeOut, NULL);
  145.         SetDisplayPlayState(gDisplayObject, kDisplayPlayStateActive);
  146.         //• SHOW SPLASH SCREEN HERE
  147.         FadeDisplayGamma(NULL, kSmoothFadeIn, NULL);
  148.         
  149.         #if 1
  150.         {
  151.             GWorldPtr                        theGWorld;
  152.             PixMapHandle                    pixMapHandle;
  153.             TQ3PixmapDrawContextData        pixmapDrawContextData;
  154.             // Create the draw context
  155.             GetDisplayBackBuffer(gDisplayObject, &theGWorld);
  156.             pixMapHandle = GetGWorldPixMap(theGWorld);
  157.             
  158.             pixmapDrawContextData.drawContextData.clearImageMethod    = kQ3ClearMethodWithColor;
  159.             pixmapDrawContextData.drawContextData.clearImageColor.a    = 1.0;
  160.             pixmapDrawContextData.drawContextData.clearImageColor.r    = 0.0;
  161.             pixmapDrawContextData.drawContextData.clearImageColor.g    = 0.0;
  162.             pixmapDrawContextData.drawContextData.clearImageColor.b    = 0.0;
  163.             pixmapDrawContextData.drawContextData.paneState         = kQ3False;
  164.             pixmapDrawContextData.drawContextData.maskState         = kQ3False;
  165.             pixmapDrawContextData.drawContextData.doubleBufferState    = kQ3False;
  166.             
  167.             pixmapDrawContextData.pixmap.image        = GetPixBaseAddr(pixMapHandle);
  168.             pixmapDrawContextData.pixmap.width         = theConfig.width;
  169.             pixmapDrawContextData.pixmap.height        = theConfig.height;
  170.             pixmapDrawContextData.pixmap.rowBytes    = (*pixMapHandle)->rowBytes & 0x00003FFF;
  171.             pixmapDrawContextData.pixmap.pixelSize    = theActualConfig.frontBufferBestDepth;
  172.             pixmapDrawContextData.pixmap.bitOrder    = kQ3EndianBig;
  173.             pixmapDrawContextData.pixmap.byteOrder    = kQ3EndianBig;
  174.             
  175.             switch (pixmapDrawContextData.pixmap.pixelSize)
  176.             {
  177.                 case 16:
  178.                     pixmapDrawContextData.pixmap.pixelType = kQ3PixelTypeRGB16;
  179.                 break;
  180.                 
  181.                 case 32:
  182.                     pixmapDrawContextData.pixmap.pixelType = kQ3PixelTypeRGB32;
  183.                 break;
  184.                 
  185.                 default:
  186.                     assert(0);
  187.             }
  188.             
  189.             gDisplayDrawContext = Q3PixmapDrawContext_New(&pixmapDrawContextData);
  190.         }
  191.         #else
  192.         {
  193.             TQ3MacDrawContextData            macDrawContextData;
  194.             
  195.             // Create the draw context
  196.             macDrawContextData.drawContextData.clearImageMethod        = kQ3ClearMethodWithColor;
  197.             macDrawContextData.drawContextData.clearImageColor.a    = 1.0;
  198.             macDrawContextData.drawContextData.clearImageColor.r    = 0.0;
  199.             macDrawContextData.drawContextData.clearImageColor.g    = 0.0;
  200.             macDrawContextData.drawContextData.clearImageColor.b    = 0.0;
  201.             macDrawContextData.drawContextData.maskState            = kQ3False;
  202.             macDrawContextData.drawContextData.paneState            = kQ3False;
  203.             macDrawContextData.drawContextData.doubleBufferState    = kQ3False;
  204.             macDrawContextData.library                                = kQ3Mac2DLibraryNone;
  205.             macDrawContextData.viewPort                                = NULL;
  206.             macDrawContextData.grafPort                                = NULL;
  207.             
  208.             GetDisplayBackBuffer(gDisplayObject, &macDrawContextData.window);
  209.         
  210.             gDisplayDrawContext = Q3MacDrawContext_New(&macDrawContextData);
  211.         }
  212.         #endif
  213.     }
  214.     #else
  215.     {
  216.         TQ3MacDrawContextData            macDrawContextData;
  217.         
  218.         // Create the window
  219.         gDisplayWindow = GetNewCWindow(kWindID_Display, NULL, (WindowPtr)(-1));
  220.         
  221.         // Create the draw context
  222.         macDrawContextData.drawContextData.clearImageMethod        = kQ3ClearMethodWithColor;
  223.         macDrawContextData.drawContextData.clearImageColor.a    = 1.0;
  224.         macDrawContextData.drawContextData.clearImageColor.r    = 0.0;
  225.         macDrawContextData.drawContextData.clearImageColor.g    = 0.0;
  226.         macDrawContextData.drawContextData.clearImageColor.b    = 0.0;
  227.         macDrawContextData.drawContextData.maskState            = kQ3False;
  228.         macDrawContextData.library                                = kQ3Mac2DLibraryNone;
  229.         macDrawContextData.drawContextData.paneState            = kQ3True;
  230.         macDrawContextData.drawContextData.doubleBufferState    = kQ3True;
  231.         macDrawContextData.window                                = (CGrafPtr) gDisplayWindow;
  232.         
  233.         Display_GetImageArea(&macDrawContextData.drawContextData.pane);
  234.     
  235.         gDisplayDrawContext = Q3MacDrawContext_New(&macDrawContextData);
  236.     }
  237.     #endif
  238.     
  239.     Q3View_SetDrawContext(gDisplayView, gDisplayDrawContext);
  240.     
  241.     // Create the renderer
  242.     #if 1
  243.         gDisplayRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
  244.         Q3InteractiveRenderer_SetDoubleBufferBypass(gDisplayRenderer, kQ3True);
  245.     #else
  246.         gDisplayRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireframe);
  247.     #endif
  248.     
  249.     Q3View_SetRenderer(gDisplayView, gDisplayRenderer);
  250.     
  251.     // Create the camera
  252.     viewAngleCameraData.cameraData.placement.cameraLocation.x    = 0.0;
  253.     viewAngleCameraData.cameraData.placement.cameraLocation.y    = 0.0;
  254.     viewAngleCameraData.cameraData.placement.cameraLocation.z    = 0.0;
  255.     viewAngleCameraData.cameraData.placement.pointOfInterest.x    = 1.0;
  256.     viewAngleCameraData.cameraData.placement.pointOfInterest.y    = 0.0;
  257.     viewAngleCameraData.cameraData.placement.pointOfInterest.z    = 0.0;
  258.     viewAngleCameraData.cameraData.placement.upVector.x            = 0.0;
  259.     viewAngleCameraData.cameraData.placement.upVector.y            = 1.0;
  260.     viewAngleCameraData.cameraData.placement.upVector.z            = 0.0;
  261.     viewAngleCameraData.cameraData.range.hither                    = 0.2;
  262.     viewAngleCameraData.cameraData.range.yon                    = 200.0;
  263.     viewAngleCameraData.cameraData.viewPort.origin.x            = -1.0;
  264.     viewAngleCameraData.cameraData.viewPort.origin.y            = 1.0;
  265.     viewAngleCameraData.cameraData.viewPort.width                = 2.0;
  266.     viewAngleCameraData.cameraData.viewPort.height                = 2.0;
  267.     viewAngleCameraData.fov                                        = 1.2;
  268.     viewAngleCameraData.aspectRatioXToY                            = Display_GetImageAspectRatio();
  269.  
  270.     gDisplayCamera = Q3ViewAngleAspectCamera_New(&viewAngleCameraData);
  271.     
  272.     Q3View_SetCamera(gDisplayView, gDisplayCamera);
  273.     
  274.     // Create the light group
  275.     gDisplayLightGroup = Q3LightGroup_New();
  276.     Q3View_SetLightGroup(gDisplayView, gDisplayLightGroup);
  277.     
  278.     // Create the point light
  279.     pointLightData.lightData.isOn        = kQ3True;
  280.     pointLightData.lightData.brightness    = 5.0;
  281.     pointLightData.lightData.color        = white;
  282.     pointLightData.castsShadows            = kQ3True;
  283.     pointLightData.attenuation            = kQ3AttenuationTypeInverseDistance;
  284.     pointLightData.location.x            = 0.0;
  285.     pointLightData.location.y            = 0.0;
  286.     pointLightData.location.z            = 0.0;
  287.     
  288.     gDisplayPointLight = Q3PointLight_New(&pointLightData);
  289.     
  290.     Q3Group_AddObject(gDisplayLightGroup, gDisplayPointLight);
  291.     
  292.     // Create the illumination shader
  293.     #if 1
  294.         gDisplayIllumination = Q3PhongIllumination_New();
  295.     #else
  296.         gDisplayIllumination = Q3LambertIllumination_New();
  297.     #endif
  298. }
  299.  
  300.  
  301. /* =============================================================================
  302.  *        Display_Exit (external)
  303.  *
  304.  *    Takes down the window, disposes of the QD3D stuff.
  305.  * ========================================================================== */
  306. void Display_Exit(
  307.     void)
  308. {
  309.     if (gDisplayHasRendered)
  310.     {
  311.         Q3Renderer_Sync(gDisplayRenderer, gDisplayView);
  312.     }
  313.     
  314.     #if USE_DRAW_SPROCKET
  315.         if(gDisplayObject != NULL)
  316.         {
  317.             FadeDisplayGamma(NULL, kSmoothFadeOut, NULL);
  318.             SetDisplayPlayState(gDisplayObject, kDisplayPlayStateInactive);
  319.             FadeDisplayGamma(NULL, kSmoothFadeIn, NULL);
  320.             DisposeDisplay(gDisplayObject);
  321.             gDisplayObject = NULL;
  322.         }
  323.     #else
  324.         if (gDisplayWindow != NULL)
  325.         {
  326.             DisposeWindow(gDisplayWindow);
  327.             gDisplayWindow = NULL;
  328.         }
  329.     #endif
  330.     
  331.     if (gDisplayView != NULL)
  332.     {
  333.         Q3Object_Dispose(gDisplayView);
  334.         gDisplayView = NULL;
  335.     }
  336.     
  337.     if (gDisplayDrawContext != NULL)
  338.     {
  339.         Q3Object_Dispose(gDisplayDrawContext);
  340.         gDisplayDrawContext = NULL;
  341.     }
  342.     
  343.     if (gDisplayRenderer != NULL)
  344.     {
  345.         Q3Object_Dispose(gDisplayRenderer);
  346.         gDisplayRenderer = NULL;
  347.     }
  348.     
  349.     if (gDisplayCamera != NULL)
  350.     {
  351.         Q3Object_Dispose(gDisplayCamera);
  352.         gDisplayCamera = NULL;
  353.     }
  354.     
  355.     if (gDisplayLightGroup != NULL)
  356.     {
  357.         Q3Object_Dispose(gDisplayLightGroup);
  358.         gDisplayLightGroup = NULL;
  359.     }
  360.     
  361.     if (gDisplayAmbientLight != NULL)
  362.     {
  363.         Q3Object_Dispose(gDisplayAmbientLight);
  364.         gDisplayAmbientLight = NULL;
  365.     }
  366.     
  367.     if (gDisplayDirectionalLight != NULL)
  368.     {
  369.         Q3Object_Dispose(gDisplayDirectionalLight);
  370.         gDisplayDirectionalLight = NULL;
  371.     }
  372.     
  373.     if (gDisplayPointLight != NULL)
  374.     {
  375.         Q3Object_Dispose(gDisplayPointLight);
  376.         gDisplayPointLight = NULL;
  377.     }
  378.     
  379.     if (gDisplayIllumination != NULL)
  380.     {
  381.         Q3Object_Dispose(gDisplayIllumination);
  382.         gDisplayIllumination = NULL;
  383.     }
  384. }
  385.  
  386.  
  387. /* =============================================================================
  388.  *        Display_Activate (external)
  389.  *
  390.  *    Handles activation and deactivation.
  391.  * ========================================================================== */
  392. void Display_Activate(
  393.     Boolean            inActivate)
  394. {
  395.     if (gDisplayActive != inActivate)
  396.     {
  397.         gDisplayActive = inActivate;
  398.         
  399.         Input_Activate(inActivate);
  400.         
  401.         if (!gDisplayActive)
  402.         {
  403.             Game_Silence();
  404.         }
  405.         
  406.         if (gDisplayHasRendered)
  407.         {
  408.             Q3Renderer_Sync(gDisplayRenderer, gDisplayView);
  409.         }
  410.         
  411.         Display_DrawGrow();
  412.     }
  413. }
  414.  
  415.  
  416. /* =============================================================================
  417.  *        Display_IsActive (external)
  418.  *
  419.  *    Returns true if the game is active.
  420.  * ========================================================================== */
  421. Boolean Display_IsActive(
  422.     void)
  423. {
  424.     #if USE_DRAW_SPROCKET
  425.         return true;
  426.     #else
  427.         return gDisplayActive;
  428.     #endif
  429. }
  430.  
  431.  
  432. /* =============================================================================
  433.  *        Display_DrawGrow (external)
  434.  *
  435.  *    Draws the grow box.
  436.  * ========================================================================== */
  437. void Display_DrawGrow(
  438.     void)
  439. {
  440. #if !USE_DRAW_SPROCKET
  441.     Rect                bounds;
  442.     
  443.     SetPort(gDisplayWindow);
  444.     
  445.     // Erase the horizontal part
  446.     bounds = gDisplayWindow->portRect;
  447.     bounds.top = bounds.bottom-15;
  448.     
  449.     EraseRect(&bounds);
  450.     
  451.     // Draw the grow box, but only the horizontal scroll stuff
  452.     ClipRect(&bounds);
  453.     DrawGrowIcon(gDisplayWindow);
  454.     ClipRect(&gDisplayWindow->portRect);
  455. #endif
  456. }
  457.  
  458.  
  459. /* =============================================================================
  460.  *        Display_DrawContents (external)
  461.  *
  462.  *    Draws the contents of the window.
  463.  * ========================================================================== */
  464. void Display_DrawContents(
  465.     void)
  466. {
  467.     TQ3Area            area;
  468.     FontInfo        fontInfo;
  469.     Rect            bounds;
  470.     Rect            bounds1;
  471.     
  472.     static Str255 pausedString = "\pPaused";  //• SHOULD COME FROM RESOURCE
  473.     
  474.     switch (Game_GetState())
  475.     {
  476.         case kGameState_Playing:
  477.             Display_RenderImage();
  478.         break;
  479.         
  480.         case kGameState_Paused:
  481.             Display_RenderImage();
  482.             Q3Renderer_Sync(gDisplayRenderer, gDisplayView);
  483.             
  484.             Display_BeginQD();
  485.             
  486.             Display_GetImageArea(&area);
  487.             
  488.             TextFont(geneva);
  489.             TextFace(bold);
  490.             TextSize(72);
  491.             
  492.             GetFontInfo(&fontInfo);
  493.             
  494.             MoveTo(
  495.                 area.min.x + 0.5*(area.min.x + area.max.x - StringWidth(pausedString)),
  496.                 area.min.y + 0.5*(area.min.y + area.max.y + fontInfo.ascent));
  497.             
  498.             ForeColor(redColor);
  499.             DrawString(pausedString);
  500.             ForeColor(blackColor);
  501.             
  502.             Display_EndQD();
  503.         break;
  504.         
  505.         case kGameState_Stopped:
  506.             Display_BeginQD();
  507.             
  508.             Display_GetImageArea(&area);
  509.             
  510.             bounds.top    = area.min.y;
  511.             bounds.left   = area.min.x;
  512.             bounds.bottom = area.max.y;
  513.             bounds.right  = area.max.x;
  514.             
  515.             PaintRect(&bounds);
  516.             
  517.             bounds1 = (*gDisplayStartScreen)->picFrame;
  518.             OffsetRect(
  519.                     &bounds1,
  520.                     ((bounds.right-bounds.left) - (bounds1.right-bounds1.left) >> 1) - bounds1.left,
  521.                     ((bounds.bottom-bounds.top) - (bounds1.bottom-bounds1.top) >> 1) - bounds1.top);
  522.             
  523.             DrawPicture(gDisplayStartScreen, &bounds1);
  524.             
  525.             Display_EndQD();
  526.         break;
  527.     }
  528. }
  529.  
  530.  
  531. /* =============================================================================
  532.  *        Display_RenderImage (internal)
  533.  *
  534.  *    Draws the 3D part of the window.
  535.  * ========================================================================== */
  536. void Display_RenderImage(
  537.     void)
  538. {
  539. #if USE_DRAW_SPROCKET
  540.     {
  541.         GWorldPtr theGWorld;
  542.         
  543.         GetDisplayBackBuffer(gDisplayObject, &theGWorld);
  544.         Q3MacDrawContext_SetWindow(gDisplayDrawContext, theGWorld);
  545.     }
  546. #endif
  547.  
  548.     Q3View_StartRendering(gDisplayView);
  549.     do
  550.     {
  551.         // Set up the rendering state
  552.         Q3InterpolationStyle_Submit(kQ3InterpolationStyleVertex, gDisplayView);
  553.         Q3BackfacingStyle_Submit(kQ3BackfacingStyleFlip, gDisplayView);
  554.         Q3FillStyle_Submit(kQ3FillStyleFilled, gDisplayView);
  555.         Q3Object_Submit(gDisplayIllumination, gDisplayView);
  556.         
  557.         // Draw the game contents
  558.         Game_Submit(gDisplayView);
  559.     }
  560.     while (Q3View_EndRendering(gDisplayView) == kQ3ViewStatusRetraverse);
  561.     
  562.     gDisplayHasRendered = true;
  563.  
  564. #if USE_DRAW_SPROCKET
  565.     SwapDisplayBuffers(gDisplayObject, Display_IsBusy, 0);
  566. #endif
  567. }
  568.  
  569.  
  570. /* =============================================================================
  571.  *        Display_IsBusy (internal)
  572.  *
  573.  *    Called by SwapDisplayBuffers (or a VBL task that it kicks off) to determine
  574.  *    if the back buffer is finished rendering.  Since QD3D can render
  575.  *    asynchronously through hardware, we have to let it finish.  Unfortunately,
  576.  *    the only way to do this is to hang here with a renderer Sync call.
  577.  * ========================================================================== */
  578. #if USE_DRAW_SPROCKET
  579.     Boolean Display_IsBusy(
  580.         DisplayObject        inDisplayObject,
  581.         UInt32                inContext)
  582.     {
  583.         Q3Renderer_Sync(gDisplayRenderer, gDisplayView);
  584.         return false;
  585.     }
  586. #endif
  587.  
  588.  
  589. /* =============================================================================
  590.  *        Display_Resize (external)
  591.  *
  592.  *    Called after the window has changed size.
  593.  * ========================================================================== */
  594. void Display_Resize(
  595.     void)
  596. {
  597. #if !USE_DRAW_SPROCKET
  598.     TQ3Area                    imageArea;
  599.     
  600.     // Reset the clip
  601.     ClipRect(&gDisplayWindow->portRect);
  602.     
  603.     // Change the draw context
  604.     Display_GetImageArea(&imageArea);
  605.     Q3DrawContext_SetPane(gDisplayDrawContext, &imageArea);
  606.     
  607.     // Change the camera aspect ratio
  608.     Q3ViewAngleAspectCamera_SetAspectRatio(gDisplayCamera, Display_GetImageAspectRatio());
  609. #endif
  610. }
  611.  
  612.  
  613. /* =============================================================================
  614.  *        Display_GetWindow (external)
  615.  *
  616.  *    Returns our window pointer.
  617.  * ========================================================================== */
  618. WindowPtr Display_GetWindow(
  619.     void)
  620. {
  621. #if USE_DRAW_SPROCKET
  622.     return NULL;
  623. #else
  624.     return gDisplayWindow;
  625. #endif
  626. }
  627.  
  628.  
  629. /* =============================================================================
  630.  *        Display_SetViewerPosition (external)
  631.  *
  632.  *    Moves the camera and point light based on the given info.
  633.  * ========================================================================== */
  634. void Display_SetViewerPosition(
  635.     const TQ3Point3D*        inPosition,
  636.     const TQ3Vector3D*        inDirection,
  637.     const TQ3Vector3D*        inUp)
  638. {
  639.     TQ3CameraPlacement        placement;
  640.     
  641.     assert(inPosition != NULL);
  642.     assert(inDirection != NULL);
  643.     assert(inUp != NULL);
  644.     
  645.     // Move the camera
  646.     placement.cameraLocation    = *inPosition;
  647.     placement.upVector            = *inUp;
  648.     
  649.     Q3Point3D_Vector3D_Add(inPosition, inDirection, &placement.pointOfInterest);
  650.     
  651.     Q3Camera_SetPlacement(gDisplayCamera, &placement);
  652.     
  653.     // Move the point light
  654.     Q3PointLight_SetLocation(gDisplayPointLight, inPosition);
  655. }
  656.  
  657.  
  658. /* =============================================================================
  659.  *        Display_GetViewerPosition (external)
  660.  *
  661.  *    Returns the current camera position.
  662.  * ========================================================================== */
  663. void Display_GetViewerPosition(
  664.     TQ3Point3D*                outPosition,
  665.     TQ3Vector3D*            outDirection,
  666.     TQ3Vector3D*            outUp)
  667. {
  668.     TQ3CameraPlacement        placement;
  669.     
  670.     assert(outPosition != NULL);
  671.     assert(outDirection != NULL);
  672.     assert(outUp != NULL);
  673.     
  674.     Q3Camera_GetPlacement(gDisplayCamera, &placement);
  675.     
  676.     *outPosition = placement.cameraLocation;
  677.     *outUp = placement.upVector;
  678.     
  679.     Q3Point3D_Subtract(
  680.             &placement.pointOfInterest,
  681.             &placement.cameraLocation,
  682.             outDirection);
  683. }
  684.  
  685.  
  686. /* =============================================================================
  687.  *        Display_GetImageArea (internal)
  688.  *
  689.  *    Sets outArea to the area occupied by the 3D image part of the window.
  690.  * ========================================================================== */
  691. void Display_GetImageArea(
  692.     TQ3Area*                outArea)
  693. {
  694.     assert(outArea != NULL);
  695.  
  696. #if USE_DRAW_SPROCKET
  697.     {
  698.         DisplayConfig theConfig;
  699.         
  700.         GetDisplayConfig(gDisplayObject, &theConfig);
  701.         
  702.         outArea->min.x = 0;
  703.         outArea->max.x = theConfig.width;
  704.         outArea->min.y = 0;
  705.         outArea->max.y = theConfig.height;
  706.     }
  707. #else    
  708.     outArea->min.x = gDisplayWindow->portRect.left;
  709.     outArea->max.x = gDisplayWindow->portRect.right;
  710.     outArea->min.y = gDisplayWindow->portRect.top;
  711.     outArea->max.y = gDisplayWindow->portRect.bottom-15;
  712. #endif
  713. }
  714.  
  715.  
  716. /* =============================================================================
  717.  *        Display_GetImageAspectRatio (internal)
  718.  *
  719.  *    Returns the aspect ratio of the area occupied by the 3D image part of the
  720.  *    window.
  721.  * ========================================================================== */
  722. float Display_GetImageAspectRatio(
  723.     void)
  724. {
  725.     TQ3Area                    imageArea;
  726.     
  727.     Display_GetImageArea(&imageArea);
  728.     
  729.     return (imageArea.max.x-imageArea.min.x) / (imageArea.max.y-imageArea.min.y);
  730. }
  731.  
  732.  
  733. /* =============================================================================
  734.  *        Display_BeginQD (internal)
  735.  *
  736.  *    Sets up for doing Quickdraw drawing.
  737.  * ========================================================================== */
  738. void Display_BeginQD(
  739.     void)
  740. {
  741. #if USE_DRAW_SPROCKET
  742.     ••• TODO •••
  743. #else    
  744.     SetPort(gDisplayWindow);
  745. #endif
  746. }
  747.  
  748.  
  749. /* =============================================================================
  750.  *        Display_EndQD (internal)
  751.  *
  752.  *    Undoes what Display_BeginQD did.
  753.  * ========================================================================== */
  754. void Display_EndQD(
  755.     void)
  756. {
  757. #if USE_DRAW_SPROCKET
  758.     ••• TODO •••
  759. #else    
  760.     // do nothing
  761. #endif
  762. }
  763.  
  764.  
  765.